﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;




public partial class product_card_user_Application : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);




    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox9.Text = "" + System.DateTime.Now.ToString();
        String saving_acc = Session["savingAccountNo"] as String;
        this.AccountNo.Text = saving_acc;
        this.AccountNo.Enabled = false;

        con.Open();

        String custId = Session["userId"] as String;

        

        SqlCommand cmd = new SqlCommand("select * from CUSTOMER where custID='" + custId + "'", con);


        cmd.Connection = con;

        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {

            N.Text = dr["fName"].ToString();

            TextBox5.Text = dr["lName"].ToString();

            NRIC.Text = dr["custNRIC"].ToString();

            Email.Text = dr["email"].ToString();
            Home.Text = dr["custTel"].ToString();
            Mobile.Text = dr["custMobile"].ToString();
            TextBox10.Text = dr["custNationality"].ToString();
            TextBox11.Text = dr["custGender"].ToString();
            DateTime birth =(DateTime)dr["custDob"];
            TextBox6.Text = birth.ToString("dd/MM/yyyy");
         
            PostalCode.Text = dr["custPostal"].ToString();
            TextBox7.Text = dr["custAddress"].ToString();

        }



        dr.Close();

        con.Close();



    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        con.Open();
        String custId = Session["userId"] as String;
        String saving_acc = Session["savingAccountNo"] as String;
        this.AccountNo.Text = saving_acc;
        this.AccountNo.Enabled = false;
        var gengerator = new AccountsManager();
        string cardNo = gengerator.GenerateNo("CARDS");
        Label5.Text = "PENDING";

        String sql = "insert into APPLICATIONDEBIT (custID,frontname,firstname,lastname,home,office,mobile,email,nation,NRIC,country,dob,gender,postalCode,jobTittle,employer,accountNo,applicationDate,status) values (@custID,@frontname,@firstname,@lastname,@home,@office,@mobile,@email,@nation,@NRIC,@country,@dob,@gender,@postalCode,@jobTittle,@employer,@accountNo,@applicationDate,@status)";
      SqlCommand cmd = new SqlCommand(sql, con);
      cmd.Parameters.AddWithValue("@custID", custId);
      cmd.Parameters.AddWithValue("@frontname", DropDownList1.SelectedValue);
      cmd.Parameters.AddWithValue("@firstname", N.Text);
      cmd.Parameters.AddWithValue("@lastname", TextBox5.Text);
      cmd.Parameters.AddWithValue("@home", Home.Text);
      cmd.Parameters.AddWithValue("@office", Office.Text);
      cmd.Parameters.AddWithValue("@mobile", Mobile.Text);
      cmd.Parameters.AddWithValue("@email", Email.Text);
      cmd.Parameters.AddWithValue("@nation", TextBox10.Text);
      cmd.Parameters.AddWithValue("@NRIC", NRIC.Text);
      cmd.Parameters.AddWithValue("@country", TextBox4.Text);
      cmd.Parameters.AddWithValue("@dob", TextBox6.Text);
      cmd.Parameters.AddWithValue("@gender", TextBox11.Text);
      cmd.Parameters.AddWithValue("@postalCode", PostalCode.Text);
      cmd.Parameters.AddWithValue("@jobTittle", JobTittle.Text);
      cmd.Parameters.AddWithValue("@employer", Employer.Text);
      cmd.Parameters.AddWithValue("@accountNo", AccountNo.Text);
      cmd.Parameters.AddWithValue("@applicationDate", TextBox9.Text);
      cmd.Parameters.AddWithValue("@status", Label5.Text);
      cmd.ExecuteNonQuery();

      String sql1 = "insert into CHARTSPENDINGS (custID, limitLeft,spentAmount) values (@custID, @limitLeft, @spentAmount)";
      SqlCommand cmd1 = new SqlCommand(sql1, con);
      cmd1.Parameters.AddWithValue("@custID", custId);
      cmd1.Parameters.AddWithValue("@limitLeft", Total.Text);
      cmd1.Parameters.AddWithValue("@spentAmount", DBNull.Value);
      cmd1.ExecuteNonQuery();

      Session["custID"] = custId;

      Response.Redirect("ApplicationSucess.aspx");
 

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        N.Text = "";
        TextBox5.Text = "";
        Home.Text = "";
        Office.Text = "";
        Mobile.Text = "";
        Email.Text = "";
        DropDownList1.Items.Clear();
        
        NRIC.Text = "";
        TextBox4.Text = "";
        PostalCode.Text = "";
        TextBox6.Text = "";
        JobTittle.Text = "";
        Employer.Text = "";
        AccountNo.Text = "";


    }

    public string custId { get; set; }



}